package day1206zy.day1206zy;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class demo6 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        List<String> list = FileUtils.readLines(new File("src/day1206zy/data1.txt"));
        List<String> list1 = FileUtils.readLines(new File("src/day1206zy/data.txt"));
        Class<?> aClass = Class.forName(list.get(0));
//        获取类对象
        Constructor<?> constructor = aClass.getConstructor();
//        获取类中构造方法
        Object o = constructor.newInstance();
//        根据构造方法创建对象
        for (int i = 0; i < list1.size(); i++) {
//            遍历list1集合
            String[] split = list1.get(i).split("=");
//            切割，创建数组
            if (split[0].equals("id")){
//                判断条件是否相同于id
                Method setId = aClass.getMethod("setId",String.class);
//                获取类中方法，string.class参数
                setId.invoke(o,split[1]);
//                实例化对象，相等于new person
            }else if (split[0].equals("name")){
                Method setName = aClass.getMethod("setName", String.class);
                setName.invoke(o,split[1]);
            }else if (split[0].equals("age")){
                Method setAge = aClass.getMethod("setAge", int.class);
                setAge.invoke(o,Integer.parseInt(split[1]));
            }
        }
        Method toString = aClass.getMethod("toString");
        Object invoke = toString.invoke(o);
        System.out.println(invoke);

    }
}
